"""This module defines several database utility functions."""

__author__ = "Gabriel Taubman (gtaubman@mac.com)"

import sqlite3 as sqlite
import md5
from models import *

def CreateUser(firstname, lastname,
               username, email,
               password, is_admin):
  u = User()
  u.firstname = firstname
  u.lastname = lastname
  u.username = username
  u.email = email
  u.password = md5.new(password).hexdigest()
  u.is_admin = is_admin
  return u

def CreatePlayer(owner, version, name,
                 rank, enabled, file_name, file_data):
  p = Player()
  p.owner = owner
  p.owner_id = owner.id
  p.version = version
  p.name = name
  p.rank = rank
  p.enabled = enabled
  p.file_name = file_name
  p.file_data = file_data
  return p

def PlayersToValidate(session):
  """Returns a list of all enabled players who haven't been validated."""
  return (session.query(Player).filter(Player.enabled == True).
          filter(Player.status == 0).all())

def ValidPlayers(session):
  """Returns a list of all players who are ready to play."""
  return (session.query(Player).filter(Player.status == 1).
          filter(Player.enabled == True).all())
